﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

using MP.Core.Config;
using MP.Core.Library;
public partial class Admin_Hurman_Controls_AddEditJobTitle : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.JOBTITLE);
        if (!IsPostBack)
        {
            loadData();
        }
    }

    public Guid Id
    {
        get { return !string.IsNullOrEmpty(Request["Id"]) ? Util.IsGuid(Request["Id"]) ? new Guid(Request["Id"]) : Guid.Empty : Guid.Empty; }
    }

    HrJobTitleEntity getJob()
    {
        HrJobTitleEntity job = new HrJobTitleEntity();
        job.Id = Guid.NewGuid();
        job.JobName = Filter.GetMaxString(txtJobTitle.Text.Trim(), HrJobTitleFields.JobCode.MaxLength);
        job.JobCode = Filter.GetMaxString(txtJobCode.Text.Trim(), HrJobTitleFields.JobName.MaxLength);
        job.Priority = 10;
        job.UpdatedBy = Util.CurrentUserName;
        job.UpdatedDate = DateTime.Now;
        return job;
    }

    void loadData()
    {
        HrJobTitleEntity job = HrJobTitleManager.CreateInstant().GetById(Id);
        if (job != null)
        {
            if (!IsEdit())
                RedirectLogin();

            txtJobCode.Text = job.JobCode;
            txtJobTitle.Text = job.JobName;
            txtDescription.Text = job.Description;

            btnAdd.Visible = false;
            btnEdit.Visible = true;
        }
        else
        {
            if (!IsAdd())
                RedirectLogin();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            HrJobTitleEntity job = getJob();
            job.CreatedBy = Util.CurrentUserName;
            job.CreatedDate = DateTime.Now;

            HrJobTitleManager.CreateInstant().Insert(job);
            Response.Redirect("/Admin/Hurman/ListJobTitle.aspx");
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            HrJobTitleEntity job = getJob();
            HrJobTitleEntity tmp = HrJobTitleManager.CreateInstant().GetById(Id);
            if (tmp != null)
            {
                job.Id = tmp.Id;
                job.CreatedBy = tmp.CreatedBy;
                job.CreatedDate = tmp.CreatedDate;
                job.Priority = tmp.Priority;

                HrJobTitleManager.CreateInstant().Update(job);
                Response.Redirect("/Admin/Hurman/ListJobTitle.aspx");
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Hurman/ListJobTitle.aspx");
    }

    protected void validJob_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        HrJobTitleEntity job = HrJobTitleManager.CreateInstant().GetById(Id);
        if (job != null) // Edit
        {
            if (job.JobCode != txtJobCode.Text.Trim() && HrJobTitleManager.CreateInstant().IsExistsByJobCode(txtJobCode.Text.Trim()))
            {
                validJob.ErrorMessage = "Trùng mã chức vụ";
                args.IsValid = false;
            }
        }
        else // Add
        {
            if (HrJobTitleManager.CreateInstant().IsExistsByJobCode(txtJobCode.Text.Trim()))
            {
                validJob.ErrorMessage = "Trùng mã chức vụ";
                args.IsValid = false;
            }
        }
    }
    
}
